package com.cdtu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.entity.RUserSpeciality;
import com.cdtu.entity.Speciality;
import com.cdtu.entity.SpecialityType;
import com.cdtu.mapper.SpecialityMapper;
import com.cdtu.mapper.SpecialityTypeMapper;
import com.cdtu.service.RUserSpecialityService;
import com.cdtu.mapper.RUserSpecialityMapper;
import com.cdtu.service.SpecialityTypeService;
import com.cdtu.vo.SpecialityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Sakura
* @description 针对表【r_user_speciality】的数据库操作Service实现
* @createDate 2023-01-13 16:15:49
*/
@Service
public class RUserSpecialityServiceImpl extends ServiceImpl<RUserSpecialityMapper, RUserSpeciality>
    implements RUserSpecialityService{

    @Autowired
    private SpecialityMapper specialityMapper;

    @Autowired
    private SpecialityTypeService specialityTypeService;

    @Autowired
    private SpecialityTypeMapper specialityTypeMapper;

    @Autowired
    private RUserSpecialityMapper rUserSpecialityMapper;

    @Override
    public List<Long> findByUserId(long user_id) {
        return rUserSpecialityMapper.findByUserId(user_id);
    }

    @Override
    public boolean addUserSpeciality(List<SpecialityVO> SpecialityVOS, long uid) {

        for (SpecialityVO vo : SpecialityVOS) {
            String sName = vo.getName();
            String typeName = vo.getType();

            RUserSpeciality rUserSpeciality=new RUserSpeciality();
            rUserSpeciality.setUserId(uid);

            SpecialityType specialityType = specialityTypeMapper.findAllByName(typeName);
            long tid=System.currentTimeMillis();
            long sid= System.currentTimeMillis();

            if (specialityType==null){
                specialityType=new SpecialityType();
                specialityType.setId(tid);
                specialityType.setName(typeName);
                boolean result = specialityTypeService.save(specialityType);
                if (!result){
                    return false;
                }

                Speciality speciality=new Speciality();
                speciality.setId(sid);
                speciality.setName(sName);
                speciality.setTypeId(tid);
                int insert = specialityMapper.insert(speciality);
                if (insert<1){
                    return false;
                }
                rUserSpeciality.setSpecialityId(sid);
                rUserSpeciality.setDetial(vo.getDetail());

                insert = rUserSpecialityMapper.insert(rUserSpeciality);
                if (insert<1){
                    return false;
                }
            }else {
                Speciality speciality = specialityMapper.findAllByName(sName);
                if (speciality==null){
                    speciality=new Speciality();
                    speciality.setId(sid);
                    speciality.setName(sName);
                    speciality.setTypeId(specialityType.getId());
                    int insert = specialityMapper.insert(speciality);
                    if (insert<1){
                        return false;
                    }
                    rUserSpeciality.setSpecialityId(sid);
                    rUserSpeciality.setDetial(vo.getDetail());

                    insert = rUserSpecialityMapper.insert(rUserSpeciality);
                    if (insert<1){
                        return false;
                    }

                }else {
                    rUserSpeciality.setDetial(vo.getDetail());
                    rUserSpeciality.setSpecialityId(speciality.getId());
                    int result = rUserSpecialityMapper.insert(rUserSpeciality);
                    if (result<1){
                        return false;
                    }
                }
            }
        }
        return true;
    }
    @Override
    public boolean removeByUid(long uid) {
      return rUserSpecialityMapper.deleteByUid(uid);
    }
}
